package knapsackga;

import java.util.HashMap;
import java.util.Map;


public class Brute
{
    public static void main( String[] args )
    {
        String definition_file = "src/data/sample-24items.dat";

        Knapsack knapsack = new Knapsack( definition_file );
        FitnessFunction fitness_func = new FitnessFunctionA( knapsack );

        int max_bit = 0;
        int num_bits = knapsack.getNumberOfObjects();
        int[] bits = new int[num_bits];
        HashMap<Integer, Integer> buckets = new HashMap<Integer, Integer>();

        long start = System.currentTimeMillis();
        boolean cont = true;
        while( cont )
        {
            // We have to carry
            if( bits[0] == 1 )
            {
                int carry = 1;
                for( int i = 0; i < num_bits && carry == 1; i++ )
                {
                    if( bits[i] == 1 && carry == 1 )
                    {
                        bits[i] = 0;
                        if( bits[i + 1] == 0 )
                        {
                            bits[i + 1] = 1;
                            carry = 0;
                        }
                    }
                    else
                    {
                        bits[i] = 0;
                        carry = 0;
                    }
                }
            }
            else
            {
                bits[0] = 1;
            }

            cont = false;
            for( int i = 0; i < num_bits; i++ )
            {
                if( i > max_bit )
                {
                    max_bit = i;
                    System.out.println( "@ bit " + max_bit );
                }
                if( bits[i] == 0 )
                {
                    cont = true;
                    break;
                }
            }
            int fitness = (int) fitness_func.calculateFitness( bits );
            int cnt = 1;
            if( buckets.containsKey( fitness ) )
            {
                cnt += buckets.get( fitness );
            }
            buckets.put( fitness, cnt );
        }
        long end = System.currentTimeMillis();

        for( Map.Entry<Integer, Integer> bucket : buckets.entrySet() )
        {
            System.out.println( bucket.getKey() + "\t" + bucket.getValue() );
        }

        System.out.println( "Eval time: " + ( end - start ) );
    }

    public static String bitString( int[] bits )
    {
        StringBuilder str = new StringBuilder();

        for( int i = 0; i < bits.length; i++ )
        {
            str.append( bits[i] );
        }

        return str.toString();
    }
}
